package models;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import play.data.validation.Required;
import play.db.jpa.Model;

/**
 * Discount class
 * 
 */

@Entity
public class Discount extends Model {
		
	@Required
	public String description = null;
	
	@Required
	public Date expirationDate = null; 
	
	@Required
	public Status status = Status.PROCESS;
	public String picUrl = null;

	@ManyToOne
	public User discountCreator = null;
	
	@ManyToOne
	public User discountReceiver = null;
	
	/**
	 * public constructor
	 * 
	 */

	public Discount(final String description, 
					final Date expirationDate, 
					final String url,
					final User discountCreator,
					final User discountReceiver){

		this.description = description;
		this.expirationDate = expirationDate;
		this.picUrl = url;
		this.discountCreator = discountCreator;
		this.discountReceiver = discountReceiver;
	}
	
	public void accept() {
		status = Status.ACCEPT;
		save();
	}
	
	public void reject() {
		status = Status.REJECT;
		save();
	}
	
	public ClubUser otherPartyAsClub(User user) {
		return (ClubUser) otherParty(user);
	}
	
	public RetailUser otherPartyAsRetailer(User user) {
		return (RetailUser) otherParty(user);
	}
	
	public User otherParty(User user) {
		if(discountCreator.equals(user)) {
			return discountReceiver;
		} else if (discountReceiver.equals(user)) {
			return discountCreator;
		} else {
			return null;
		}
	}
}
